const express = require("express");
const admin = express.Router();

// const bodyParser = require("body-parser")
// admin.use( bodyParser.urlencoded({extended: true}))
// admin.use(bodyParser.json());

const querystring = require("querystring");
//引用数据库
const dbmysql= require("../model/connection")

//配置局部中间件
const mw1 = (req,res,next)=>{
    var str = "";
    req.on("data",(value)=>{
        str += value;
    });
    req.on("end",()=>{
        req.body = querystring.parse(str);
        next();
    });
}
//渲染导航栏
admin.get("/header",(req,res)=>{
    var sql = "select * from header"
    dbmysql.query(sql,(err,result)=>{
        if(err){
            return console.log(err.message)
        }
        if (req.session.admin==null) {
            return res.send({ status: 1, msg: '用户未登录' })
        }
        if(result.length>0){

            res.send({
                status: 0,
                msg: '登录成功',
                header:result,
                username:req.session.admin
                // bool:0
            });
        }else{
            //登录失败
            res.send({
                status: 1,
                msg: '登录失败,用户名或密码错误',
                admin:[]
            });
        }

    })
    // console.log("这是header")
})
function usename(){
    var sql = "sel"
}
//测试
admin.get("/aaa",(req,res)=>{
    console.log("aaaaaaaaa")
    // console.log(req.query.username)
    // console.log(req.query.password)
    // console.log(mysql.query);
    // // console.log(req.body)
    res.send("aaaaaa")

    // res.redirect("/index.html")
})

admin.post("/log",mw1,(req,res)=>{

})
//登录
admin.post("/login",mw1,(req,res)=>{
    // console.log(req.body)
    // console.log(req.body.username)
    // console.log(req.body.password)
    var sql = "select * from user where useName=? and pwd=?";
    dbmysql.query(sql,[req.body.username,req.body.password],(err,result)=>{
        //异常处理
        // console.log(err)
        // console.log(result)
        // console.log(result[0])

        if(err) console.log(err.message)
        if(result.length>0){
            //登录成功
            //把用户的登录状态改为0
            updateStatus(0,result[0]);
            // insertStatus(0,result[0]);
            //更新session中的用户状态
            result[0].status = 0;
            req.session.admin = result[0];
            //跳转到列表页面
            res.send({ status: 0, msg: '登录成功',admin:result[0]});
        }else{
            //登录失败
            res.send({ status: 1, msg: '登录失败,用户名或密码错误',admin:[]});
        }
    })
});
//更新登录状态
function updateStatus(status,admin){
    var sql = "update user set status = ? where useName = ?";
    dbmysql.query(sql,[status,admin.useName],(err,result)=>{
        if(result.affectedRows === 1){
            console.log("修改登录状态成功");
        }
    });
}
// function insertStatus(status,admin){
//     var sql = "update header set u_name = ? where h_name = '首页'";
//     dbmysql.query(sql,[admin.useName],(err,result)=>{
//         if(result.affectedRows === 1){
//             console.log("修改用户名字成功");
//         }
//     });
// }
//注册
admin.post("/add",mw1,(req,res)=>{
    console.log("add")
    // console.log(req.body)
    // console.log(req.body.username)
    // console.log(req.query)
    var bool = false;
    var sql = "select * from user where useName = ?";
    dbmysql.query(sql,[req.body.username],(err,result)=>{
        if(err) console.log(err.message);
        if(result.length==0){
            bool=true;
        }
        if(!bool){
            res.send({
                status: 1,
                msg: '此账号已存在'
            })
        }else{
            console.log(req.body)
            var sql = "insert into user values (?,?,?,?,?,?)";
            dbmysql.query(sql,[,req.body.username,req.body.pwd,req.body.mail,0,1],(err,result)=>{
                if(err) console.log(err.message)
                //成功
                console.log(result)
                if(result.affectedRows > 0){
                    // console.log(33)
                    res.send({
                        status: 0,
                        msg: '用户添加成功'
                    })
                }else{
                    // console.log(44)
                    res.send({
                        status: 1,
                        msg: '添加失败'
                    })
                }
            })
        }
    })
});
//衬衫
admin.post("/shirt",mw1,(req,res)=>{
    var sql = "select * from product"
    dbmysql.query(sql,(err,result)=>{
        if(err){
            return console.log(err.message)
        }
        // console.log(req.session.admin)
        if (req.session.admin==null) {
            return res.send({ status: 1, msg: '用户未登录' })
        }
        // console.log(result)
        if(result.length>0){

            res.send({
                header:result,
                username:req.session.admin,
                status:0
            });
        }

    })
})
//详情页
admin.post("/page",mw1,(req,res)=>{
    var sql = "select * from details where c_id=?"
    dbmysql.query(sql,[req.session.c_id.c_id],(err,result)=>{
        if (req.session.admin==null) {
            return res.send({ status: 1, msg: '用户未登录' })
        }

        if(err){
            return console.log(err.message)
        }
        // console.log(req.session.c_id.c_id)
        // let zhuan_session = req.session.c_id
        // console.log(zhuan_session.c_id)
        // console.log(req.session.admin)

        // console.log(result)
        if(result.length>0){
            res.send({
                pages:result,
                username:req.session.admin,
                status:0
            });
        }else {
            res.send({
                status: 1,
                msg: '未登录或请求错误，请重新登录',
            });
        }

    })
})
//加入购物车
admin.post("/s_join",mw1,(req,res)=>{
    // console.log(req.body)
    var sql = "insert into shop values(?,?,?,?,?,?,?,?,?)"
    dbmysql.query(sql,[,req.body.name,req.body.price,req.body.color,req.body.size,req.body.num,req.body.img,1,req.body.u_name],(err,result)=>{
        if (req.session.admin==null) {
            return res.send({ status: 1, msg: '用户未登录' })
        }
        // console.log(result)
        if(err){
            return console.log(err.message)
        }

        if(result.affectedRows==1){
            res.send({
                shopping:result,
                username:req.session.admin,
                status:0
            });
        }
        console.log(result);
    })
})

admin.post("/shopping",mw1,(req,res)=>{
    // console.log()
    // console.log(req.body);
    var sql = "select * from shop where u_name = ?"
    dbmysql.query(sql,[req.session.admin.useName],(err,result)=>{
        if (req.session.admin==null) {
            return res.send({ status: 1, msg: '用户未登录' })
        }
        //有错误输出错误
        if(err){
            return console.log(err.message)
        }
        //计算总数
        var zong = 0
        for(let i=0;i<result.length;i++){
             zong +=result[i].s_price * result[i].s_num
        }

        // console.log(result)
        res.send({
            shop:result,
            // unbool:bools.result,
            username:req.session.admin,
            shop_zong:zong,
            status:0
        });
    })
})
//购物车数量减少
admin.post("/sub",mw1,(req,res)=>{
    // console.log(req.body);
    // console.log(req.body.sub_num);
    // console.log(req.body.sub_name);
    // console.log(req.body.sub_color);
    // console.log(req.body.sub_size);
    var sql = "update shop set s_num=? where s_name=? and s_color=? and s_size=?"
    dbmysql.query(sql,[ req.body.sub_num,req.body.sub_name,req.body.sub_color,req.body.sub_size],(err,result)=> {
        // if(result.affectedRows === 1){
        //     console.log("数量减一成功");
        // }
    })
})
//购物车数量增加
admin.post("/plus",mw1,(req,res)=>{
    // console.log(req.body);
    // console.log(req.body.plus_num);
    // console.log(req.body.plus_name);
    // console.log(req.body.plus_color);
    // console.log(req.body.plus_size);
    var sql = "update shop set s_num=? where s_name=? and s_color=? and s_size=?"
    dbmysql.query(sql,[req.body.plus_num,req.body.plus_name, req.body.plus_color, req.body.plus_size],(err,result)=> {
        // if(result.affectedRows === 1){
        //     console.log("数量加一成功");
        // }
    })
})
//购物车伪删除
admin.post("/del",mw1,(req,res)=>{
    // console.log(req.body);
    // console.log(req.body.del_name);
    // console.log(req.body.del_color);
    // console.log(req.body.del_size);
    var sql = "update shop set status=? where s_name=? and s_color=? and s_size=?"
    dbmysql.query(sql,[0,req.body.del_name, req.body.del_color, req.body.del_size],(err,result)=> {
        if(result.affectedRows === 1){
            console.log("伪删除成功");
        }
    })
})
//衬衫页面跳转详情页路由
admin.get("/shirt_box",(req,res)=>{

    var sql ="select c_id from product where p_name =?"
    dbmysql.query(sql,[req.query.shirt_name],(err,result)=>{
        // console.log(result[0]);
        // console.log(req.query);

        //将商品编码存到session
        req.session.c_id = result[0];

        if(result.length>0){
            res.send({
                status:0
            });
        }else {
            res.send({
                status: 1,
                msg: '未登录或请求错误，请重新登录',
            });
        }
    })
    // console.log(res.query);
})
//订单页显示用户名字
admin.post("/orderpage",mw1,(req,res)=>{
    var sql = "select * from records where user_id = ?"
    dbmysql.query(sql,[req.session.admin.useName],(err,result)=>{
        if (req.session.admin==null) {
            return res.send({ status: 1, msg: '用户未登录' })
        }
        // console.log(result)
        if(err){
            return console.log(err.message)
        }

        res.send({
            shopping:result,
            username:req.session.admin,
            status:0
        });

        console.log(result);
    })
})
var a
//当点击为ture
admin.post("/arr_true",mw1,(req,res)=>{
    console.log(req.body);
    console.log(req.session.admin);
    console.log(a++)
    // var sql = "insert into records values(?,?,?,?,?,?,?,?,?)"
    // dbmysql.query(sql,[,req.session.admin.useName,req.body.name,req.body.size,req.body.color,req.body.sum,req.body.xiaoji,req.body.img,a++],(err,result)=>{
    //     if (req.session.admin==null) {
    //         return res.send({ status: 1, msg: '用户未登录' })
    //     }
    //     // console.log(result)
    //     if(err){
    //         return console.log(err.message)
    //     }
    //
    //     if(result.affectedRows==1){
    //         res.send({
    //             shopping:result,
    //             username:req.session.admin,
    //             status:0
    //         });
    //     }
    //     console.log(result);
    // })
})

module.exports = admin;